//初始化
var ua = "h5";
var userobj={};
//初始化用户信息
var initUser = function(){
    var accessId = getParameter("x-access-id");
    var accessToken = getParameter("x-access-token");
    if(accessId && accessToken){
        userobj.accessId=accessId;
        userobj.accessToken=accessToken;
        userobjOld = JSON.parse(localStorage.getItem("userobj"));
        if(userobjOld && userobjOld.accessId==accessId){
            userobjOld.accessToken = accessToken;
            userobj = userobjOld;
        }
        localStorage.setItem("userobj", JSON.stringify(userobj));
    }else{
        userobj = localStorage.getItem("userobj") ? getUser() : {};
    }
}
//刷新资金账号信息
var refreshCapitalAccount = function(){
    $.ajax({
        url:genUrl("/wallet/getCapitalAccount"),
        type: 'GET',
        contentType:"application/json",
        dataType:'json',
        async:false,
        headers:{
            "x-access-id": userobj.accessId,
            "x-access-token": userobj.accessToken,
            "x-ua": ua
                     },
        success: function (res) {
            if(res.code==0){
                var data=res.data;
                var capitalAccount=data.list[0];
                userobj.capitalAccount = JSON.stringify(capitalAccount);
                userobj.gainsBal = data.gainsBal;
                userobj.gains = data.gains;
                userobj.ruleUrl = data.ruleUrl;
                userobj.customerPhoneCheckStatus = data.customerPhoneCheckStatus;
                updateUser(userobj);
            }else{
                console.log("资金账户获取失败！");
            }
        }
    });
}

var updateUser = function(user){
    localStorage.setItem("userobj",JSON.stringify(user));
}

var getUser = function(){
    return JSON.parse(localStorage.getItem("userobj"));
}

var genUrl = function (uri) {
    var domain = document.domain;
    if(domain.indexOf("local")>-1){
        domain=domain+":8080";
    }else{
        domain=domain;
    }
    var apiMode = "/api";
    if(document.domain.indexOf("local")>-1 || document.domain.indexOf("test")>-1){
        apiMode = "/api1";
    }
    return document.location.protocol + "//"  + domain + apiMode + uri;
}